package client.vue;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import codebase.IJoueur;

public class Tab extends JPanel {

	private JTextArea zone_conversation;
	private JScrollPane scrollpane;
	private JTextPane zone_ecriture;
	private JPanel actions;
	private JPanel zone_bas;
	private String tabName;
	private int index;

	public Tab(String name, JTabbedPane tabbedPane, final ArrayList<String> listeJoueursChat,
			int index) {

		final IJoueur joueur = client.vue.Jeu.getJoueur();

		this.index = index;

		tabName = name;
		setLayout(new BorderLayout());
		zone_conversation = new JTextArea();
		scrollpane = new JScrollPane(zone_conversation);
		zone_ecriture = new JTextPane();
		actions = new JPanel();
		zone_bas = new JPanel();
		scrollpane.setPreferredSize(new Dimension(200, 415));
		zone_conversation.addMouseWheelListener(scrollpane.getMouseWheelListeners()[0]);
		zone_conversation.setMinimumSize(new Dimension(200, 415));

		zone_conversation.setEditable(false);
		zone_conversation.setLineWrap(true);

		zone_ecriture.setPreferredSize(new Dimension(getWidth() - 101, 99));
		// on organise la zone du bas

		zone_bas.setLayout(new BorderLayout());
		zone_bas.add(zone_ecriture, BorderLayout.EAST);

		actions.setPreferredSize(new Dimension(100, 99));
		actions.setLayout(new BorderLayout());
		JButton bouton_envoyer = new JButton("Envoyer");
		JButton bouton_vider = new JButton("Vider");
		bouton_vider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				zone_conversation.setText("");
			}
		});
		actions.add(bouton_vider, BorderLayout.NORTH);
		actions.add(bouton_envoyer, BorderLayout.CENTER);
		zone_bas.add(actions, BorderLayout.EAST);
		zone_bas.add(zone_ecriture, BorderLayout.CENTER);
		add(zone_bas, BorderLayout.SOUTH);
		add(scrollpane, BorderLayout.CENTER);

		zone_ecriture.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar() == '\n' || e.getKeyChar() == '\r') {
					if (!e.isShiftDown()) {
						// lorsqu'on tape sur entree sans avoir Shift enfonce,
						// on envoie le message et on vide la zone d'ecriture
						try {
							client.vue.Jeu.getPartie().sendMessage(
									joueur.getNom() + " dit : " + zone_ecriture.getText(), tabName,
									listeJoueursChat);
						} catch (RemoteException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						zone_ecriture.setText("");
					} else {
						zone_ecriture.setText(zone_ecriture.getText() + "\n");
					}
				}
			}

		});

		bouton_envoyer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// On recupere le message dans la zone d'ecriture et on l'envoie
				try {
					client.vue.Jeu.getPartie().sendMessage(
							joueur.getNom() + " dit : " + zone_ecriture.getText(), tabName,
							listeJoueursChat);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				zone_ecriture.setText("");
			}
		});

	}

	public void refresh(String msg) {
		zone_conversation.setText(zone_conversation.getText() + "\r\n" + msg);
	}

	public String getTabName() {
		return tabName;
	}

	public int getIndex() {
		return index;
	}

}
